const KoaRouter = require('koa-router');
const router = new KoaRouter();
const { User } = require('../database/index')

router.prefix('/personal');

router.
  post('/create', async ctx =>{
    let { floderName, imagePath, childrens, setTime, floderPath, username } = ctx.request.body
    let floder =  new User({
      floderName,
      imagePath,
      childrens,
      setTime,
      floderPath
    })

    // 添加
    floders.push(floder)

    let result = await User.findOneAndUpdate(
      {
        username
      },{
        floders
      }
    )

    if( result === undefined) {
      ctx.body = {
        code: 40004,
        msg: '新建文件夹失败'
      }
    }else {
      ctx.body = {
        code: 20000,
        msg: '成功新建文件夹 '+ floderName,
        result
      }
    }
  })
  .post('/getList', async ctx => {
    let { username } = ctx.request.body
    // 获取floders
    let result = await User.find({
      username
    }).select('-_id floders');

    let floderList= result[0]
    ctx.response.body = {
      floderList,
      code: 20000,
      msg: '获取个人文件列表成功'
    }
  })

  module.exports.personalRouter = router